### Replication file for
# Title: From Foe to Friend? Government-Opposition Conflict and the Appointment of Cabinet Ministers
# Authors: Herzog, Alexander (alexander.herzog@uni-bamberg.de ; University of Bamberg); Schmuck, David (david.schmuck@uni-bamberg.de ; University of Bamberg , corresponding author)
# Journal: Political Science Research and Methods

# APPENDIX D Word Loadings

# Pre-Settings ----
# Set working directory
# Note: Set working directory for log and input file accordingly
setwd("")

# Clear environment
rm(list = ls(all = TRUE))

# Load packages
library(tidyverse) # for data manipulation
library(scales) # for graph 

# Load data file
load("Replication data - PSRM - Herzog Schmuck - From Foe to Friend - Appendix D Word Loadings.RData")

# Figure D.1: Distribution of word-loadings for all words and across all cabinets included in the analysis
figD1 <- ggplot(word_loadings, aes(x = loading)) +
  theme_bw(base_size = 18) +
  geom_histogram(binwidth = 0.05, fill = "white", color = "black") +
  coord_cartesian(xlim = c(-2.5, 2.5),
                  ylim = c(0, 140000)) +
  scale_x_continuous(breaks = seq(-2.5, 2.5, by = 0.5)) +
  scale_y_continuous(labels = comma, breaks = seq(0, 140000, by = 20000)) +
  xlab("Word loadings") +
  ylab("Count")

# Plot the graph
plot(figD1)

# Save plot
ggsave(
  filename = "Herzog_Schmuck_FigD1_Word_loadings_all.tiff",
  device='tiff',
  dpi = 200,
  width = 11,
  height = 9.89,
  units = "in",
  compression = "lzw"
)


# Figure D.2: Distribution of word-loadings for selected words across all cabinets included in the analysis

# Select words indicating government and opposition language
selected_words <- c("regierung",
                    "regierungspartei",
                    "regierungsparteien",
                    "kanzler",
                    "kanzlerin",
                    "bundeskanzl",
                    "bundeskanzlerin",
                    "ministerr",
                    "ministerin",
                    "opposit",
                    "oppositionspartei",
                    "oppositionsparteien",
                    "oppositionsfrakt",
                    "oppositionsfraktionen",
                    "regierungskoali",
                    "regierungskoalit",
                    "regierungsfrakt",
                    "regierungsfraktionen")

word_loadings_selected <- word_loadings |>
  filter(word %in% selected_words) |>
  mutate(word = case_when(
    word == "regierung"  ~ "regierung",
    word %in% c("regierungskoali", "regierungskoalit") ~ "regierungskoalition",
    word %in% c("regierungsfrakt", "regierungsfraktionen") ~ "regierungsfraktion(en)",
    word %in% c("regierungspartei", "regierungsparteien") ~ "regierungspartei(en)",
    word %in% c("kanzler", "kanzlerin", "bundeskanzl", "bundeskanzlerin") ~ "(bundes)kanzler(in)",
    word %in% c("ministerr", "ministerin") ~ "minister(in)",
    word == "opposit"  ~ "opposition",
    word %in% c("oppositionspartei", "oppositionsparteien") ~ "oppositionspartei(en)",
    word %in% c("oppositionsfrakt", "oppositionsfraktionen") ~ "oppositionsfraktion(en)"
  ))

figD2 <- ggplot(word_loadings_selected, aes(x = loading)) +
  theme_bw(base_size = 18) +
  facet_wrap(~ word) +
  geom_histogram(binwidth = 0.05, fill = "white", color = "black") +
  #geom_density() +
  geom_vline(aes(xintercept = 0), linetype = "solid", linewidth = 0.5) +
  coord_cartesian(xlim = c(-0.6, 0.6),
                  ylim = c(0, 15)) +
  scale_x_continuous(breaks = round(seq(-0.6, 0.6, by = 0.2), 2)) +
  scale_y_continuous(breaks = seq(0, 15, by = 5)) +
  xlab("Word loadings") +
  ylab("Count")

# Plot the graph
plot(figD2)

# Save plot
ggsave(
  filename = "Herzog_Schmuck_FigD2_Word_loadings_selected.tiff",
  device='tiff',
  dpi = 200,
  width = 11,
  height = 9.89,
  units = "in",
  compression = "lzw"
)
